package com.springbootes.service.impl;

import com.alibaba.fastjson.JSON;
import com.springbootes.pojo.Content;
import com.springbootes.service.IndexService;
import com.springbootes.utils.HtmlParseUitl;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @User 小菜鸡
 * @Data 2020/5/17
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //从网页中爬取数据，并保存到es中
    public boolean insert(String keyword) {
        BulkResponse bulk = null;
        try {
            //抓取关键字的数据
            List<Content> list = HtmlParseUitl.parseContent(keyword);
            //创建批处理对象
            BulkRequest bulkRequest = new BulkRequest();
            //将索引请求对象添加到批处理对象当中
            for (int i = 0; i < list.size(); i++) {
                //只要指定索引，会自动生成文档id和文档类型
                bulkRequest.add(new IndexRequest("jd").source(JSON.toJSONString(list.get(i)), XContentType.JSON));
            }
            //将数据批量添加到es中
            bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return !bulk.hasFailures();
        }
    }

    //从es中取出爬出的数据，返回给前端
    public List<Map<String, Object>> select(Integer pageCur, Integer pageSize) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        //构建SearchRequest对象，初始化索引值
        SearchRequest searchRequest = new SearchRequest("jd");
        //构建SearchSourceBuilder对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "java");
        sourceBuilder.query(termQueryBuilder);
        //为查询结果分页，初始页
        sourceBuilder.from(pageCur);
        //每页的数据量
        sourceBuilder.size(pageSize);
        searchRequest.source(sourceBuilder);
        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : searchResponse.getHits().getHits()) {
                //hit.getSourceAsMap()返回的是一个Map<String, Object>类型的map集合
                list.add(hit.getSourceAsMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
